package com.shycloud.mido.member.controller;

import com.shycloud.mido.member.api.vo.MultiAssistVO;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.member.api.entity.MultiAssist;
import com.shycloud.mido.member.api.util.Constant;
import com.shycloud.mido.member.api.vo.MultiAssistRecordVO;
import com.shycloud.mido.member.service.MultiAssistRecordService;
import com.shycloud.mido.member.service.MultiAssistService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2021-07-12
 */
@RestController
@RequestMapping("/v1/multi-assist")
@Api(tags = "学生消费版-天天帮")
public class MultiAssistController {

	@Autowired
	private MultiAssistService multiAssistService;

	@Autowired
	private MultiAssistRecordService multiAssistRecordService;

	/**
	 * @param vo
	 * @return
	 * @throws Exception
	 * @Description: assign
	 * @author baiqiang.
	 * @date 2021年7月14日 上午11:06:28
	 * @version V1.0
	 */
	@ApiOperation("分配天天帮")
	@PostMapping("/assign")
	public R<Boolean> assign(@RequestBody MultiAssistRecordVO vo) throws Exception {

		multiAssistService.assign(vo);

		return new R<Boolean>(true, "操作成功");
	}

	@ApiOperation("开通天天帮（学生）")
	@PostMapping("/opening")
	public R<Boolean> opening(@RequestBody MultiAssistRecordVO vo) throws Exception {

		multiAssistService.opening(vo);

		return new R<Boolean>(true, "操作成功");
	}

	@ApiOperation("查询天天帮")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "subject", value = "1总部2代理商3机构4工作室5老师6学生", paramType = "query", dataType = "string",
					required = true),
			@ApiImplicitParam(name = "memberId", value = "当前登录用户ID", paramType = "query",
					dataType = "string", required = true)})
	@GetMapping("/query")
	public R<MultiAssist> query(String subject, String memberId) throws Exception {
		MultiAssist vo = new MultiAssist();
		vo.setSubject(subject);
		if (StringUtils.equalsAny(subject, Constant.ASSIST_SUBJECT_MASTER, Constant.ASSIST_SUBJECT_OPT)) {
			vo.setOptId(Integer.valueOf(memberId));
		} else if (StringUtils.equalsAny(subject, Constant.ASSIST_SUBJECT_ORG, Constant.ASSIST_SUBJECT_STUDIO)) {
			vo.setOrgId(Integer.valueOf(memberId));
		} else if (StringUtils.equals(subject, Constant.ASSIST_SUBJECT_TEACHER)) {
			vo.setTeacherId(memberId);
		} else if (StringUtils.equals(subject, Constant.ASSIST_SUBJECT_STUDENT)) {
			vo.setStudentId(memberId);
		}
		MultiAssist one = multiAssistService.getOne(Wrappers.<MultiAssist>lambdaQuery(vo));
		return new R<MultiAssist>(one);
	}

	@ApiOperation("APP-查询天天帮权限")
	@ApiImplicitParam(name = "studentId", value = "学生ID", paramType = "query", dataType = "string", required = true)
	@GetMapping("/getAssistPermission")
	public R<MultiAssistVO> getAssistPermission(String studentId) throws Exception {

		MultiAssistVO multiAssistVO = new MultiAssistVO();

		MultiAssist multiAssist = queryFeign(Constant.ASSIST_SUBJECT_STUDENT, studentId);

		if (multiAssist != null) {
			BeanUtils.copyProperties(multiAssist, multiAssistVO);
		}

		return new R(multiAssistVO);
	}


	@ApiOperation("分页查询天天帮记录(总部，代理商，机构，工作室)")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int", required = true),
			@ApiImplicitParam(name = "size", value = "每页显示条数", paramType = "query", dataType = "int", required = true),
			@ApiImplicitParam(name = "commonId", value = "机构ID代理商ID老师ID学生ID", paramType = "query", dataType = "String", required = false),
			@ApiImplicitParam(name = "subjectLaunch", value = "1总部2代理商3机构4工作室5老师6学生", paramType = "query", dataType = "String", required = false),
			@ApiImplicitParam(name = "fromName", value = "名称", paramType = "query", dataType = "String", required = false),
			@ApiImplicitParam(name = "fromPhone", value = "手机号", paramType = "query", dataType = "String", required = false)
	})
	@GetMapping("/pageMultiAssistRecord")
	public R<IPage<Map<String, Object>>> pageMultiAssistRecord(Long current, Long size, MultiAssistRecordVO vo) throws Exception {
		Page<Map<String, Object>> page = new Page<>(current, size);
		IPage<Map<String, Object>> result = multiAssistRecordService.pageMultiAssistRecordVO(page, vo);
		return new R<IPage<Map<String, Object>>>(result);
	}

	@ApiOperation("feign - 查询天天帮")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "subject", value = "1总部2代理商3机构4工作室5老师6学生", paramType = "query", dataType = "string",
					required = true),
			@ApiImplicitParam(name = "memberId", value = "当前登录用户ID", paramType = "query",
					dataType = "string", required = true)})
	@GetMapping("/queryFeign")
	public MultiAssist queryFeign(String subject, String memberId) throws Exception {
		MultiAssist vo = new MultiAssist();
		vo.setSubject(subject);
		if (StringUtils.equalsAny(subject, Constant.ASSIST_SUBJECT_MASTER, Constant.ASSIST_SUBJECT_OPT)) {
			vo.setOptId(Integer.valueOf(memberId));
		} else if (StringUtils.equalsAny(subject, Constant.ASSIST_SUBJECT_ORG, Constant.ASSIST_SUBJECT_STUDIO)) {
			vo.setOrgId(Integer.valueOf(memberId));
		} else if (StringUtils.equals(subject, Constant.ASSIST_SUBJECT_TEACHER)) {
			vo.setTeacherId(memberId);
		} else if (StringUtils.equals(subject, Constant.ASSIST_SUBJECT_STUDENT)) {
			vo.setStudentId(memberId);
		}
		return multiAssistService.getOne(Wrappers.<MultiAssist>lambdaQuery(vo));
	}

}
